****migration-related moments. Things need to be adjusted so that it's appropriate to 
***draw equally from each state




set seed 1234

qui{
forval i = 1/1000{
    
	use "$temp/acs_master_kid_sample", clear
	if mod(`i', 20)==0{
		noi di as result "`i'"
	}
	bsample 100000
	

	su move if !coll [fw=perwt]
	local movencoll = `r(mean)'
	su move if coll [fw=perwt]
	local movecoll = `r(mean)'
	
	su hc [fw=perwt] if move & !coll 
	local mean1 =`r(mean)'
	su hc [fw=perwt] if !move & !coll  //difference of 0.236 to target!
	local mean2 = `r(mean)'
	local diffncoll = `mean1' - `mean2'

	su hc [fw=perwt] if move  & coll
	local mean1 =`r(mean)'
	su hc [fw=perwt] if !move & coll  //difference of 0.236 to target!
	local mean2 = `r(mean)'
	local diffcoll = `mean1' - `mean2'
	
	
	
	***outlfow rates
	preserve
	collapse (mean) move [fweight = perwt], by(bpl)
	save "$temp/state_outflow_boot_`i'", replace
	restore
	
	preserve
	keep if move
	gen count = 1
	collapse (sum) count [fw=perwt], by(statefips)
	egen grandcount = total(count)
	gen frac = count/grand
	keep frac statefips
	save "$temp/state_inflow_boot_`i'", replace
	restore
	
	preserve
	gen stay = (move == 0)
	collapse (mean) stay [fw = perwt], by(bpl)
	su stay
	local stay_mean = `r(mean)'
	local stay_sd = `r(sd)'
	restore
	
	
	*****share of moves to proximal states*****
	use "$temp/state_prox_mat", clear
	reshape long v, i(stfips) j(bpl)
	ren stfips statefips
	save "$temp/state_prox_mat_long", replace

	//merge to stay migration rates
	use "$temp/state_mig_matrix", clear
	reshape long st_dum_, i(bpl) j(statefips)
	merge 1:1 bpl statefips using "$temp/state_prox_mat_long", keep(match) nogen

	//ease interpretation: multiply all by 2k
	replace st_dum_ = st_dum_ * 2000
	drop if bpl == statefips
	ren v prox
	collapse (sum) st_dum_, by(prox) //sum up all migrants broken down by whether move is proximal
	su st if !prox
	local val0 = `r(mean)'
	su st if prox
	local val1 = `r(mean)'
	local prox = `val1' / (`val0' + `val1')
	
	clear
	set obs 7
	gen moment = .
	replace moment = `movencoll' in 1
	replace moment = `movecoll' in 2
	replace moment = `diffncoll' in 3
	replace moment = `diffcoll' in 4	
	replace moment = `stay_mean' in 5
	replace moment = `stay_sd' in 6
	replace moment = `prox' in 7
	xpose, clear
	save "$temp/boot/mig_moments_boot_`i'", replace		
}
}
/*
exit
clear
forval i = 1/1000{
    append using "$temp/boot/mig_mat_boot_`i'"
}
export delimited "$dir/Model/boot/mig_mat_boot_sample.csv", novarn replace
*/

clear
forval i = 1/1000{
    append using "$temp/boot/mig_moments_boot_`i'"
}
export delimited "$dir/Model/boot/mig_moments_boot_sample.csv", novarn replace




//get SDs for moments related to inflow/outflow
clear
forval i = 1/1000{
    use "$temp/state_outflow_boot_`i'", clear
	drop bpl
	xpose, clear
	save "$temp/state_outflow_boot_x_`i'", replace
	
	use "$temp/state_inflow_boot_`i'", clear
	drop statefips
	xpose, clear
	save "$temp/state_inflow_boot_x_`i'", replace
}

clear
forval i = 1/1000{
	append using "$temp/state_outflow_boot_x_`i'"
}
export delimited "$dir/Model/boot/outflow_boot_sample.csv", novarn replace

clear
forval i = 1/1000{
	append using "$temp/state_inflow_boot_x_`i'"
}
export delimited "$dir/Model/boot/inflow_boot_sample.csv", novarn replace





//end of do-file
